package com.study.hadoop.mapreduce.mapJoin;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author huang
 * @Date 2022/1/20 14:07
 * @Version 1.0
 */
public class MapJoinMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
    private Map<String, String> pdMap = new HashMap();
    private String line;
    private String[] strs;
    private String pname;
    private Text outK = new Text();
    @Override
    protected void setup(Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
        //获取缓存文件，将文件封装到集合pd.txt
        URI[] cacheFiles = context.getCacheFiles();
        FileSystem fileSystem = FileSystem.get(context.getConfiguration());
        FSDataInputStream open = fileSystem.open(new Path(cacheFiles[0]));
        BufferedReader buffer = new BufferedReader(new InputStreamReader(open, StandardCharsets.UTF_8));
        String line;
        while (StringUtils.isNotEmpty(line = buffer.readLine())) {
            String[] strs = line.split("\t");
            pdMap.put(strs[0], strs[1]);
        }
        IOUtils.closeStream(buffer);
    }

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
        line = value.toString();
        strs = line.split("\t");
        pname = pdMap.get(strs[1]);
        outK.set(strs[0]+pname+strs[2]);
        context.write(outK,NullWritable.get());
    }
}
